package com.hao.tool.web.module.base.controller;

import com.hao.tool.common.enums.ShortEnum;
import com.hao.tool.web.module.base.entity.ShortInfo;
import com.hao.tool.web.module.base.service.ShortInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * description
 *
 * @author RuiHaoZ on GZ.cmhk
 * @date 2019-11-22 23:37
 */
@Controller
@RequestMapping("/s")
public class Short2Controller {


    @Autowired
    private ShortInfoService shortInfoService;


    @GetMapping("")
    public ModelAndView buildQr() {
        String modelValue = "/short/index";
        return new ModelAndView(modelValue);
    }

    @GetMapping("/{shortId}")
    public ModelAndView buildQr(@PathVariable String shortId) {
        ShortInfo shortInfo = shortInfoService.getById(shortId);
        String modelValue = "/common/404";
        if (shortInfo != null) {
            ShortEnum shortEnum = ShortEnum.getByCode(shortInfo.getShortType());
            modelValue = "redirect:/"
                    + shortEnum.getRestUrl().replace("{id}", shortInfo.getLongId());
        }
        return new ModelAndView(modelValue);
    }
}
